// 通知管理
const Service = require('egg').Service

class NoticeService extends Service {
  // 发送通知
  async createNotice(senderId, receiverId, message) {
    // 创建通知
    const { app } = this;

    // 插入通知数据
    const result = await app.mysql.insert('notice', {
      sender_id: senderId,
      receiver_id: receiverId,
      message: message,
      status: 'pending',
    });

    return result;
  }

  // 获取通知列表
  async getNotice(userId) {
    const { app } = this;

    // 获取用户的所有通知
    const notice = await app.mysql.select('notice', {
      where: { receiver_id: userId },
      orders: [['created_at', 'desc']],
    });

    return notice;
  }

  // 更新通知状态
  async updateStatus(noticeId, status) {
    const { app } = this;

    // 更新状态
    const result = await app.mysql.update('notice', { status }, {
      where: { id: noticeId },
    });

    return result;
  }
}

module.exports = NoticeService;
